package com.ljye.jg.ljye.fragment;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.jaeger.ninegridimageview.NineGridImageView;
import com.jaeger.ninegridimageview.NineGridImageViewAdapter;
import com.ljye.jg.ljye.R;
import com.ljye.jg.ljye.activity.Activity_ImageBrowse;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.util.ArrayList;
import java.util.List;

import me.hwang.widgets.SmartPullableLayout;

public class Fragment_Circle_1 extends Fragment {


    private static final int ON_REFRESH = 1;
    private static final int ON_LOAD_MORE = 2;
    private View view;
    private SmartPullableLayout mPullableLayout;
    private List<String> urls_list;
    private ListView listView;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ON_REFRESH:
//                    listData.add(0, "新的数据");
//                    mLvAdapter.notifyDataSetChanged();
                    mPullableLayout.stopPullBehavior();
                    break;
                case ON_LOAD_MORE:
//                    listData.add("加载数据");
//                    mLvAdapter.notifyDataSetChanged();
                    mPullableLayout.stopPullBehavior();
                    break;
            }
        }
    };
    private NineGridImageViewAdapter<String> mAdapter = new NineGridImageViewAdapter<String>() {
        @Override
        protected void onDisplayImage(Context context, final ImageView imageView, String url) {
//            -------------------------------------------- 使图片缩放
            Transformation transformation = new Transformation() {

                @Override
                public Bitmap transform(Bitmap source) {

                    int targetWidth = imageView.getWidth();
//                    LogCat.i("source.getHeight()="+source.getHeight()+",source.getWidth()="+source.getWidth()+",targetWidth="+targetWidth);

                    if (source.getWidth() == 0) {
                        return source;
                    }

                    //如果图片小于设置的宽度，则返回原图
                    if (source.getWidth() < targetWidth) {
                        return source;
                    } else {
                        //如果图片大小大于等于设置的宽度，则按照设置的宽度比例来缩放
                        double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                        int targetHeight = (int) (targetWidth * aspectRatio);
                        if (targetHeight != 0 && targetWidth != 0) {
                            Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                            if (result != source) {
                                // Same bitmap is returned if sizes are the same
                                source.recycle();
                            }
                            return result;
                        } else {
                            return source;
                        }
                    }

                }

                @Override
                public String key() {
                    return "transformation" + " desiredWidth";
                }

            };
//------------------------------------------------
            Picasso.with(context)
                    .load(url)
                    .placeholder(R.drawable.ic_default_image)
                    .transform(transformation)
                    .into(imageView);
        }


        @Override
        protected void onItemImageClick(Context context, int index, List list) {
            super.onItemImageClick(context, index, list);
//            Toast.makeText(context, "" + index, Toast.LENGTH_SHORT).show();
            ArrayList<String> imgs = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                imgs.add(list.get(i) + "");
//                Log.d("MainActivity", list.get(i) + "");
            }
            Intent intent = new Intent(getActivity(), Activity_ImageBrowse.class);
            intent.putExtra("position", index);
            intent.putStringArrayListExtra("imgs", imgs);
            startActivity(intent);
        }

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = inflater.inflate(R.layout.fragment_class_management2, container, false);
            listView = (ListView) view.findViewById(R.id.lv_content);
            urls_list = new ArrayList<>();
            urls_list.add("http://ww3.sinaimg.cn/large/610dc034jw1f5d36vpqyuj20zk0qo7fc.jpg");
            urls_list.add("http://ww3.sinaimg.cn/large/610dc034jw1f5byokn81tj20dw0hiwfe.jpg");
            urls_list.add("http://ww1.sinaimg.cn/large/7a8aed7bjw1f2sm0ns82hj20f00l8tb9.jpg");
            urls_list.add("http://ww3.sinaimg.cn/large/7a8aed7bjw1f2x7vxkj0uj20d10mi42r.jpg");
            urls_list.add("http://ww4.sinaimg.cn/large/7a8aed7bjw1eyaov0c9z4j20iz0sg40t.jpg");
            urls_list.add("http://ww4.sinaimg.cn/large/7a8aed7bjw1exr0p4r0h3j20oy15445o.jpg");
            urls_list.add("http://ww3.sinaimg.cn/large/610dc034jw1f5t889dhpoj20f00mi414.jpg");
            urls_list.add("http://ww3.sinaimg.cn/large/7a8aed7bgw1ew38eojcpzj20p010kwjr.jpg");
            urls_list.add("http://ww4.sinaimg.cn/large/7a8aed7bgw1eu22nypk9hj20dw0kutc0.jpg");
            urls_list.add("http://ww3.sinaimg.cn/large/7a8aed7bjw1ezbriom623j20hs0qoadv.jpg");

            listView.setAdapter(new MyAdapter());

            mPullableLayout = (SmartPullableLayout) view.findViewById(R.id.layout_pullable);
            mPullableLayout.setOnPullListener(new SmartPullableLayout.OnPullListener() {
                @Override
                public void onPullDown() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            mHandler.sendEmptyMessage(ON_REFRESH);
                        }
                    }).start();
                }

                @Override
                public void onPullUp() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            mHandler.sendEmptyMessage(ON_LOAD_MORE);
                        }
                    }).start();
                }
            });
        }
        return view;
    }

    static class ViewHolder {
        public NineGridImageView<String> iv_icon;
    }

    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return urls_list == null ? 0 : urls_list.size();
        }

        @Override
        public Object getItem(int i) {
            return urls_list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(parent.getContext(), R.layout.circle_list_view, null);
                ViewHolder holder = new ViewHolder();
                //控件，封装到holder身上
                holder.iv_icon = (NineGridImageView<String>) convertView.findViewById(R.id.ngiv_nine_grid);
//                holder.iv_icon.setShowStyle(NineGridImageView.STYLE_GRID);//网格布局
//                holder.iv_icon.setShowStyle(NineGridImageView.STYLE_FILL);//铺满布局
                convertView.setTag(holder);
            }
            ViewHolder holder = (ViewHolder) convertView.getTag();
            //把当前条目的数据放到控件上显示
            if (urls_list.size() == 0) {
                holder.iv_icon.setVisibility(View.GONE);
            } else {
                holder.iv_icon.setAdapter(mAdapter);
                holder.iv_icon.setImagesData(urls_list);
            }
            return convertView;
        }
    }


}
